﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.货号管理.service;
using DevExpress.XtraGrid;
using DevExpress.Data;
using DXApplicationTangche.UC.货号管理.销售款式;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.套号管理.套号下单;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.套号管理.经销商下单;

namespace DXApplicationTangche.UC.货号管理
{
    public partial class XtraUserControl样衣登记一览 : DevExpress.XtraEditors.XtraUserControl
    {
        #region 变量
        private String saleStyleCode;//销售款式
        private List<VO销售样衣> VO销售样衣s = new List<VO销售样衣>();//销售样衣的页面VO
        private Model销售样衣 model;
        private DTO销售款式 dTO销售款式 = new DTO销售款式();
        #endregion
        public XtraUserControl样衣登记一览()
        {
            InitializeComponent();
        }
        private void simpleButton刷新_Click(object sender, EventArgs e)
        {
            this.refreshstyle();
            this.清空();
        }
        /// <summary>
        /// 查询款式信息
        /// </summary>
        private void refreshstyle()
        {
            if (String.IsNullOrEmpty(this.comboBox款式标签.Text))
            {
                this.comboBox款式标签.SelectedItem = this.comboBox款式标签.Items[0];
            }
            //查询销售款式一览，并根据款式描述显示销售款式内容
            this.model = new Model销售样衣().build款式标签筛选显示(
                    货号管理Service.get销售款式一览(0, 1000),
                    this.comboBox款式标签.SelectedItem.ToString()
                    );
            this.gridControl销售款式.DataSource = this.model.款式标签销售款式s;
            this.bandedGridView销售款式.RefreshData();
        }
        /// <summary>
        /// 查询款式下样衣及销售记录
        /// </summary>
        private void refreshGrid()
        {
            //从DTO获取数据，绑定数据
            //API只要一行，原子性
            //接口查询，并在model进行DTO和页面VO转化  
            this.VO销售样衣s = new Model销售样衣(this.saleStyleCode).VO销售样衣s;
            this.gridControl销售样衣.DataSource = this.VO销售样衣s;
            this.advBandedGridView销售样衣.RefreshData();
            DTO工厂门店套号销售出库 DTO工厂门店套号销售出库 = 货号管理Service.getStyleSaleInfo(this.saleStyleCode);
            this.gridControl门店套号销售出库.DataSource = DTO工厂门店套号销售出库.stockInOutDetailDTOSShopSaleOut;
            this.gridControl门店套号最近14天销售出库.DataSource = DTO工厂门店套号销售出库.stockInOutDetailDTOSShopSaleOutRecent;
            this.gridControl工厂套号销售出库.DataSource = DTO工厂门店套号销售出库.stockInOutDetailDTOSFactorySaleOut;
            this.gridControl工厂套号最近14天销售出库.DataSource = DTO工厂门店套号销售出库.stockInOutDetailDTOSFactorySaleOutRecent;
            this.advBandedGridView销售样衣.GroupSummary.Clear();
            this.advBandedGridView销售样衣.GroupSummary.Add(new GridGroupSummaryItem(SummaryItemType.Count, "inventorySuffix", null, "(合计 = {0} 条)"));
        }
        private void 清空()
        {
            this.gridControl销售样衣.DataSource = null;
            this.gridControl门店套号销售出库.DataSource = null;
            this.gridControl门店套号最近14天销售出库.DataSource = null;
            this.gridControl工厂套号销售出库.DataSource = null;
            this.gridControl工厂套号最近14天销售出库.DataSource = null;
            this.advBandedGridView销售样衣.RefreshData();
            this.gridView门店套号销售出库.RefreshData();
            this.gridView门店套号最近14天销售出库.RefreshData();
            this.gridView工厂套号销售出库.RefreshData();
            this.gridView工厂套号最近14天销售出库.RefreshData();
        }

        private void simpleButton样衣登记_Click(object sender, EventArgs e)
        {
            new Form样衣登记().ShowDialog();
            //new Form销售款式维护().ShowDialog();
            this.refreshstyle();
            this.清空();
        }

        
        /// <summary>
        /// 查询销售款式下的样衣及销售出库记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void advBandedGridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            this.saleStyleCode = FunctionHelper.GetbandedgridViewValue(
                                    this.bandedGridView销售款式,
                                    this.bandedGridView销售款式.FocusedRowHandle,
                                    "saleStyleCode");
            String id = FunctionHelper.GetbandedgridViewValue(
                                    this.bandedGridView销售款式,
                                    this.bandedGridView销售款式.FocusedRowHandle,
                                    "id");
            foreach  (DTO销售款式 item in this.model.款式标签销售款式s)
            {
                if (id.Equals(item.id))
                {
                    this.dTO销售款式 = item;
                    break;
                }
            }
            this.refreshGrid();
        }

        private void comboBox款式标签_SelectedIndexChanged(object sender, EventArgs e)
        {
            //根据选择的销售标签筛选出匹配数据
            this.refreshstyle();
            this.清空();
        }
        private void 款式废弃ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String id = FunctionHelper.GetbandedgridViewValue(
                                        this.bandedGridView销售款式,
                                        this.bandedGridView销售款式.FocusedRowHandle,
                                        "id");
            DialogResult dialogResult = MessageBox.Show("是否废弃？", "是", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                if (!String.IsNullOrWhiteSpace(货号管理Service.deprecateSaleStyle(id)))
                {
                    MessageBox.Show("操作成功");
                    this.refreshstyle();
                    this.清空();
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
            }
        }
        private void 货号作废ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "saleInventoryMatialId");
            DialogResult dialogResult = MessageBox.Show("是否废弃？", "是", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                if (!String.IsNullOrWhiteSpace(货号管理Service.deprecateInventoryNo(saleInventoryMatialId)))
                {
                    MessageBox.Show("操作成功");
                    this.refreshGrid();
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
            }
        }
        private void 辅料维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
            //                            this.advBandedGridView销售样衣,
            //                            this.advBandedGridView销售样衣.FocusedRowHandle,
            //                            "stockInventoryNo");
            //String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
            //                            this.advBandedGridView销售样衣,
            //                            this.advBandedGridView销售样衣.FocusedRowHandle,
            //                            "saleInventoryMatialId");
            //new Form销售样衣辅料维护(stockInventoryNo, saleInventoryMatialId).ShowDialog();
            //this.refreshGrid();
        }

        private void 上传示例图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "saleInventoryMatialId");
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String str = 货号管理Service.saveSaleInventoryMatialId(saleInventoryMatialId, ofd);
                if (!String.IsNullOrWhiteSpace(str))
                {
                    this.refreshGrid();
                }
            }
        }

        private void 电商下单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //成衣码
            String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "stockInventoryNo");
            //货号面料码
            String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "saleInventoryMatialId");
            if (new Model销售样衣().build样衣辅料(this.VO销售样衣s, saleInventoryMatialId).count <= 0)
            {
                DialogResult dialogResult = MessageBox.Show("该样衣无辅料清单，是否继续下单？", "是", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            new Form电商下单(stockInventoryNo,"电商").ShowDialog();
        }

        private void 门店套号二次下单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //成衣码
            String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "stockInventoryNo");
            //货号面料码
            String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "saleInventoryMatialId");
            if (new Model销售样衣().build样衣辅料(this.VO销售样衣s, saleInventoryMatialId).count <= 0)
            {
                DialogResult dialogResult = MessageBox.Show("该样衣无辅料清单，是否继续下单？", "是", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
            new Form套号销售样衣下单(stockInventoryNo, this.splashScreenManager).ShowDialog();
        }

        private void simpleButton经销商下单_Click(object sender, EventArgs e)
        {
            new Form经销商下单().ShowDialog();
        }

        private void 查询样衣登记信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //成衣码
                String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                            this.advBandedGridView销售样衣,
                                            this.advBandedGridView销售样衣.FocusedRowHandle,
                                            "stockInventoryNo");
                foreach (VO销售样衣 item in this.VO销售样衣s)
                {
                    //  成衣码匹配取得单个VO销售样衣
                    if (stockInventoryNo.Equals(item.stockInventoryNo))
                    {
                        //  将该样衣及样衣对应的款式信息传入登记页面显示
                        new Form样衣登记(this.dTO销售款式,item).ShowDialog();
                        break;
                    }
                }
                //DTO销售款式 dTO销售款式 = 货号管理Service.getSaleStyleByBarcode(stockInventoryNo);
                //SaleStyleSample saleStyleSample = 货号管理Service.getSaleStyleSampleByBarcode(stockInventoryNo);
                //new Form样衣登记(dTO销售款式, saleStyleSample).ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.refreshGrid();
        }

        private void 大货下单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //成衣码
            String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "stockInventoryNo");
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
            new Form套号销售样衣下单(stockInventoryNo,true, this.splashScreenManager).ShowDialog();
        }

        private void 款式设计维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Form款式迭代一览(
                FunctionHelper.GetbandedgridViewValue(
                    this.bandedGridView销售款式,
                    this.bandedGridView销售款式.FocusedRowHandle,
                    "saleStyleCode"),
                FunctionHelper.GetbandedgridViewValue(
                    this.bandedGridView销售款式,
                    this.bandedGridView销售款式.FocusedRowHandle,
                    "designStyleCode"),
                FunctionHelper.GetbandedgridViewValue(
                    this.bandedGridView销售款式,
                    this.bandedGridView销售款式.FocusedRowHandle,
                    "designStyleName")
                ).ShowDialog();
        }

        private void 查询标准款式设计点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Form查询标准款设计点(
                FunctionHelper.GetbandedgridViewValue(
                    this.bandedGridView销售款式,
                    this.bandedGridView销售款式.FocusedRowHandle,
                    "saleStyleCode")
                //"7774596"
                ).ShowDialog();
        }
    }
    public class VO销售样衣
    {
        public String saleInventoryId { get; set; } //  货号主键
        public int inventorySuffix { get; set; } //  货号后缀   给出货号定义年月
        public int refInventorySuffix { get; set; } //  参考货号后缀
        public String saleStyleId { get; set; } //  销售款式id 
        public String saleStyleCode { get; set; }   //  款号，销售定义
        public String saleInventoryMatialId { get; set; }   //  货号对应物料定义
        public String styleNameCN { get; set; } //  工艺款式名称
        public int materialId { get; set; } //  物料数据库ID
        public String materialCode { get; set; }    //  物料编码
        public String materialDesc { get; set; }  //  物料描述
        public String materialComposition { get; set; } //  成分
        public String vendorName { get; set; }  //  供应商
        public String materialImagePath { get; set; } //  物料图片路径
        public List<StyleAccessoryDTO> styleAccessoryDTOS { get; set; }   //  辅料清单
        public String stockInventoryNo { get; set; }
        public String saleId { get; set; }  //  价格体系主键，商品定价的唯一标识
        public Double salePrice { get; set; }   //  零售价
        public String sampleImagePath { get; set; }  //  实例图片路径
        public Image sampleImage { get; set; }  //  实例图片
        public DateTime createDate { get; set; } //创建时间
        public VO销售样衣(SaleStyleSample saleStyleSample)
        {
            this.saleInventoryId = saleStyleSample.saleInventoryId;
            this.inventorySuffix = saleStyleSample.inventorySuffix;
            this.refInventorySuffix = saleStyleSample.refInventorySuffix;
            this.saleStyleId = saleStyleSample.saleStyleId;
            this.saleStyleCode = saleStyleSample.saleStyleCode;
            this.saleInventoryMatialId = saleStyleSample.saleInventoryMatialId;
            this.styleNameCN = saleStyleSample.styleNameCN;
            this.materialId = saleStyleSample.materialId;
            this.materialCode = saleStyleSample.materialCode;
            this.materialDesc = saleStyleSample.materialDesc;
            this.materialComposition = saleStyleSample.materialComposition;
            this.vendorName = saleStyleSample.vendorName;
            this.materialImagePath = saleStyleSample.materialImagePath;
            this.styleAccessoryDTOS = saleStyleSample.styleAccessoryDTOS;
            this.stockInventoryNo = saleStyleSample.stockInventoryNo;
            this.saleId = saleStyleSample.saleId;
            this.salePrice = saleStyleSample.salePrice;
            this.sampleImagePath = saleStyleSample.sampleImagePath;
            this.createDate = saleStyleSample.createDate;
            try
            {
                this.sampleImage = FileService.GetImageByUrl(URL.图片url + URL.销售样衣实例图片路径 + saleStyleSample.sampleImagePath);
            }
            catch
            {
                this.sampleImage = FunctionHelper.getSSHIRT();
            }
        }
    }
    public class Model销售样衣
    {
        public List<VO销售样衣> VO销售样衣s { get; set; } = new List<VO销售样衣>();//销售样衣的页面VO
        public List<DTO销售款式> 款式标签销售款式s { get; set; } = new List<DTO销售款式>();//根据款式描述筛选后的销售款式列表
        public DTO销售款式 dTO销售款式 { get; set; } = new DTO销售款式();
        public int count { get; set; }//辅料个数
        public Model销售样衣()
        {

        }
        /// <summary>
        /// 查询销售样衣,进行DTO和页面VO转换
        /// </summary>
        /// <param name="saleStyleSamples"></param>
        public Model销售样衣(String saleStyleCode)
        {
            List<SaleStyleSample> saleStyleSamples = 货号管理Service.getSaleStyleSample(saleStyleCode, 0, 1000);
            foreach (SaleStyleSample item in saleStyleSamples)
            {
                this.VO销售样衣s.Add(new VO销售样衣(item));
            }
        }
        /// <summary>
        /// 根据款式描述筛选出该款式描述下的款式列标
        /// </summary>
        /// <param name="DTO销售款式s"></param>
        /// <param name="styleTag"></param>
        /// <returns></returns>
        public Model销售样衣 build款式标签筛选显示(List<DTO销售款式> DTO销售款式s, String styleTag)
        {
            foreach (DTO销售款式 item in DTO销售款式s)
            {
                if (styleTag.Equals(item.styleTag))
                {
                    this.款式标签销售款式s.Add(item);
                }
            }
            return this;
        }
        /// <summary>
        /// 查询样衣中的辅料清单个数
        /// </summary>
        /// <param name="VO销售样衣s"></param>
        /// <param name="saleInventoryMatialId"></param>
        /// <returns></returns>
        public Model销售样衣 build最新样衣辅料(List<SaleStyleSample> VO销售样衣s, String saleInventoryMatialId)
        {
            foreach (SaleStyleSample item in VO销售样衣s)
            {
                if (saleInventoryMatialId.Equals(item.saleInventoryMatialId))
                {
                    this.count = item.styleAccessoryDTOS.Count;
                    break;
                }
            }
            return this;
        }
        /// <summary>
        /// 查询样衣中的辅料清单个数
        /// </summary>
        /// <param name="VO销售样衣s"></param>
        /// <param name="saleInventoryMatialId"></param>
        /// <returns></returns>
        public Model销售样衣 build样衣辅料(List<VO销售样衣> VO销售样衣s,String saleInventoryMatialId)
        {
            foreach (VO销售样衣 item in VO销售样衣s)
            {
                if (saleInventoryMatialId.Equals(item.saleInventoryMatialId))
                {
                    this.count = item.styleAccessoryDTOS.Count;
                    break;
                }
            }
            return this;
        }
        /// <summary>
        /// 样衣对应销售款
        /// </summary>
        /// <param name="saleStyleCode"></param>
        /// <returns></returns>
        public Model销售样衣 build样衣对应销售款(String saleStyleCode)
        {
            foreach (DTO销售款式 item in this.款式标签销售款式s)
            {
                if (saleStyleCode.Equals(item.saleStyleCode))
                {
                    this.dTO销售款式 = item;
                    break;
                }
            }
            return this;
        }
        /// <summary>
        /// 最新货号的转化
        /// </summary>
        /// <param name="saleStyleSamples"></param>
        public Model销售样衣(List<SaleStyleSample> saleStyleSamples)
        {
            foreach (SaleStyleSample item in saleStyleSamples)
            {
                this.VO销售样衣s.Add(new VO销售样衣(item));
            }
        }
    }
}
